package com.quanyan.stadium.service.impl;

import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.stadium.biz.BizPointsDao;
import com.quanyan.stadium.entity.TbPoints;
import com.quanyan.stadium.service.commonComponent.BaseService;
import com.quanyan.stadium.service.PointsService;
import com.quanyan.user.response.RespUserInfoBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by lei.w on 16/6/23.
 */
@Service
public class PointsServiceImpl implements PointsService {

    private static final Logger logger = LoggerFactory.getLogger(PointsServiceImpl.class);

    @Autowired
    BizPointsDao bizPointsDao;
    @Autowired
    private BaseService baseService;

    @Override
    public boolean entryUserPoints(TbPoints tbPoints) {

        TbPoints tbP = bizPointsDao.getPointsByCategoryIdAndUserIds(tbPoints.getUid(), tbPoints.getCategoryId());
        int result;
        RespUserInfoBase user = baseService.queryUserMsg(tbPoints.getUid());

        if (ObjectUtil.isNotObjectEmpty(user)) {
            tbPoints.setCityId(user.getAreaId());
        }

        if (tbP == null) {
            result = bizPointsDao.addUserPoints(tbPoints);
        } else {
            tbPoints.setId(tbP.getId());
            int score = tbPoints.getScore()+tbP.getScore();

            tbPoints.setScore(score);
            result = bizPointsDao.updateUserPoints(tbPoints);
        }
        boolean flag = result > 0;

        logger.info("entryUserPoints result："+ flag +" uid:"+tbPoints.getUid());

        return flag;
    }

    @Override
    public void batchEntryUserPoints(List<TbPoints> tbPointsList) {

    }

    @Override
    public List<TbPoints> queryPointsByCategoryIdAndUserIds(List<Integer> tbPointsList, Integer categoryId) {

        return bizPointsDao.queryUserPointsBYUserIds(tbPointsList, categoryId);
    }

    @Override
    public Integer getPointsByCategoryIdAndUserIds(Integer uid, Integer categoryId) {
        TbPoints tbPoints = bizPointsDao.getPointsByCategoryIdAndUserIds(uid, categoryId);

        if (tbPoints == null) {
            return 0;
        }

        return tbPoints.getScore();
    }
}